<%--
  Created by IntelliJ IDEA.
  User: XBB
  Date: 2021/2/18
  Time: 14:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css">

    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/dtree.css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui_ext/dtree/font/dtreefont.css">
</head>
<body>
<div style="padding: 10px">

    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>查询条件</legend>
    </fieldset>
    <form  class="layui-form">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称</label>
            <div class="layui-input-inline">
                <input type="text" name="roleName"   placeholder="请输入角色名称" autocomplete="off" class="layui-input">
            </div>
            <label class="layui-form-label">角色简介</label>
            <div class="layui-input-inline">
                <input type="text" name="roleDesc"   placeholder="请输入角色简介" autocomplete="off" class="layui-input">
            </div>
            <div class="layui-input-inline">
                <button  class="layui-btn layui-btn-normal layui-icon layui-icon-search layui-btn-radius layui-btn-sm" lay-submit lay-filter="doSearch">查询
                </button>
                <button type="reset" class="layui-btn layui-btn-warm layui-icon layui-icon-refresh layui-btn-radius layui-btn-sm">重置
                </button>
            </div>
        </div>
    </form>


    <table id="roleTable" lay-filter="roleTableFilter"></table>
</div>

<script id="toolbarrole" type="text/html">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="roleAdd">添加</button>
    </div>
</script>

<script id="barrole" type="text/html">
    <a class="layui-btn layui-btn-xs layui-btn-radius" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs layui-btn-radius" lay-event="allotMenu">分配菜单</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-radius" lay-event="del">删除</a>
</script>

<!-- 添加和修改的弹出层开始 -->
<div style="display: none;padding: 20px" id="saveOrUpdateDiv">
    <form class="layui-form" lay-filter="dataFrm" id="dataFrm">
        <div class="layui-form-item">
            <label class="layui-form-label">角色名称:</label>
            <div class="layui-input-block">
                <input type="hidden" name="roleId">
                <input type="text" name="roleName" placeholder="请输入角色名称" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">角色简介:</label>
            <div class="layui-input-block">
                <input type="text" name="roleDesc" placeholder="请输入角色简介" lay-verify="required" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">是否可用:</label>
                <div class="layui-input-inline">
                    <input type="radio" name="available" value="1" checked="checked" title="可用">
                    <input type="radio" name="available" value="0" title="不可用">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="text-align: center;padding-right: 120px">
                <button type="button" class="layui-btn layui-btn-normal layui-btn-md layui-icon layui-icon-release layui-btn-radius" lay-filter="doSubmit" lay-submit="">&nbsp; 提交</button>
                <button type="reset" class="layui-btn layui-btn-warm layui-btn-md layui-icon layui-icon-refresh layui-btn-radius">&nbsp; 重置</button>
            </div>
        </div>
    </form>
</div>

<%--角色分配菜单的弹出层开始--%>
<div style="display: none;padding: 20px" id="allotMenu">
    <div class="layui-form-item">
        <ul id="roleTree" class="dtree" data-id="0"></ul>
    </div>
</div>


<script type="text/javascript" src="${pageContext.request.contextPath}/static/layui/layui.js"></script>
<script>

    var roleTab;
    var roleIndex;
    var title;

    layui.extend({
        dtree: '${pageContext.request.contextPath}/static/layui_ext/dtree/dtree'   //{/}代表采用自有路径
    }).use(['form','layer','jquery','table','dtree'],function() {
        var form = layui.form, layer = layui.layer, $ = layui.jquery,table = layui.table , dtree = layui.dtree;

        roleTab = table.render({
            elem:"#roleTable"
            ,url:"${pageContext.request.contextPath}/sys/role/roleRightJson"
            ,cols:[[
                {type:'checkbox'}
                ,{field:'roleId',title:'角色ID',align:"center",sort:true}
                ,{field:'roleName',title:'角色名称',align:"center"}
                ,{field:'roleDesc',title:'角色简介',align:"center"}
                ,{field:'available',title:'是否可用',align:"center",templet:function (r) {
                        return r.available == 1 ? '可用':'<span style="color: red">不可用</span>'
                    }}
                ,{fixed:'right',title:'操作',align:"center",toolbar:'#barrole'}
            ]]
            ,toolbar:'#toolbarrole'
            ,page:true
            ,limit:[5,10,15]
            ,limit:5
        })

        //给查询按钮添加事件
        form.on('submit(doSearch)',function (data) {

            roleTab.reload({
                where:data.field
                ,page:{curr:1}
            })

            return false;
        })

        //监听头部工具栏
        table.on('toolbar(roleTableFilter)',function (obj) {
            if(obj.event == "roleAdd"){
                roleAdd()
            }
        })

        //点击添加按钮的弹出
        function roleAdd() {
            roleIndex = layer.open({
                type:1
                ,title:"添加角色"
                ,area:['700px','500px']
                ,content:$('#saveOrUpdateDiv')
            })
            //清除表单的内容
            $("#dataFrm").get(0).reset()
            $("#roleId").val("")
            form.render()
            title = "添加"
        }

        //给弹出层的form表单submit按钮添加点击事件
        form.on('submit(doSubmit)',function (obj) {

            layer.confirm("您确定要"+title+"吗？",function () {
                $.post("${pageContext.request.contextPath}/sys/role/roleAddOrUpdate",obj.field,function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(roleIndex)
                            //刷新table
                            roleTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }

                })
            })
            return false;
        })

        //监听行工具栏
        table.on('tool(roleTableFilter)',function (obj) {
            if(obj.event == "edit"){
                edit(obj.data)
            }else if(obj.event == "del"){
                del(obj.data)
            }else if(obj.event == "allotMenu"){
                allotMenu(obj.data)
            }
        })

        function edit(data) {
            roleIndex = layer.open({
                type:1
                ,title:"修改角色"
                ,area:['700px','500px']
                ,content:$('#saveOrUpdateDiv')
            })

            //给表单设置值
            form.val('dataFrm',data)
            $('[name="name"]').val(data.title)
            $("#roleId").val(data.parentId)
            form.render()
            title = "修改"
        }

        function del(data){
            layer.confirm("确定删除"+data.roleName+"吗？",function (i) {
                $.post("${pageContext.request.contextPath}/sys/role/roleDelete",{"id":data.roleId},function (r) {
                    if(r.code == 0){
                        layer.msg(r.msg,{
                            offset:'15px'
                            ,icon:1
                            ,time:1000
                        },function () {
                            //关闭当前弹出框
                            layer.close(roleIndex)
                            //刷新table
                            roleTab.reload({
                                page:{curr:1}
                            })
                        });
                    }else{
                        layer.msg(r.msg)
                    }

                })
            })
        }

        //分配菜单弹出层
        function allotMenu(data) {
            roleIndex = layer.open({
                type:1
                ,title:"分配[ "+data.roleName+" ]菜单"
                ,area:['500px','700px']
                ,content:$('#allotMenu')
                ,btn:['确认分配','取消分配']
                ,btnAlign: 'c'
                ,yes:function (index,layero) {

                    //获取所有选中的复选框
                    var params = dtree.getCheckbarNodesParam("roleTree");

                    var nodeIds = ""
                    for(var i = 0;i<params.length;i++){
                        var nodeId = params[i].nodeId
                        nodeIds += nodeId + ","
                    }
                    //把角色编号和所有选中的菜单传到后台
                    $.post("${pageContext.request.contextPath}/sys/role/allotMenuByRoleId",{"roleId":data.roleId,"roleIds":nodeIds},function (r) {
                        if(r == "1"){
                            layer.close(roleIndex)
                            layer.msg("分配成功",{
                                offset:'15px'
                                ,icon:1
                                ,time:1000
                            })
                        }

                    })

                    return false;
                }
                ,btn2:function(index,layero) {
                    layer.close(roleIndex)
                    return false;
                }
            })

            //初始化树
            //给dtree赋值
            dtree.render({
                elem: "#roleTree"
                ,width:"100%"
                ,url: "${pageContext.request.contextPath}/sys/role/roleLeftJson?roleId="+data.roleId // 使用url加载（可与data加载同时存在）
                ,dataStyle: "layuiStyle"  //使用layui风格的数据格式
                ,response:{message:"msg",statusCode:0}  //修改response中返回数据的定义
                ,checkbar:true
                ,checkbarType: "all"
                ,checkbarData: "choose"
                ,dataFormat:"list"
            })
        }
    })
</script>
</body>
</html>
